<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */

// ------------------------------------------------------------------------

/**
 * Report Class
 *
 * @category	Home
 */
class ReportAction extends HomeAction
{

    protected $Report;

	function _initialize()
	{
		parent::_initialize();

        $this->Report = D('Report');
	}


    //用户报告
    public function add()
    {

    	$relatedID = intval($_REQUEST['rid']);
        $type = trim($_REQUEST['type']);

    	$reasons = L('report_reasons');
    	$captcha = require_captcha_when('report', $this->_user['user_group']);
    	$result = array();

        switch ($type)
        {
        	case 'Q':
        		$result = M('Question')->find($relatedID);
        	    $message = L('question_not_existed');
        		break;
        	case 'A':
        	    $result = M('Answer')->find($relatedID);
        	    $message = L('answer_not_existed');
        	    break;
        	case 'C':
        	    $result= M('Comment')->find($relatedID);
        	    $message = L('comment_not_existed');
        	    break;
        }
        $categoryID = $result['category_id'];

        $map = array(
            'reporter_id' => $this->_user['user_id'],
            'type' => $type,
            'related_id' => $relatedID,
        );
        $count = $this->Report->where($map)->count();

    	if ($this->isPost())
    	{
    	    if (!$this->_userGroup['allow_report'])
        	{
        		$this->error(L('no_perms_action'));
        	}

        	if (empty($result))
            {
                $this->error($message);
            }

            if ($count > 0)
            {
                $this->error(L('report_duplicated'));
            }

			if ($captcha && md5($_POST['verify']) != $_SESSION['verify'])
			{
				$this->error(L('captcha_incorrect'));
			}

    	    $reasonID = intval($_POST['reason_id']);
        	$data = array(
        		'reporter_id'   => $this->_user['user_id'],
        		'reporter_name' => $this->_user['username'],
        		'category_id'   => $categoryID,
        		'type'          => $type,
        		'related_id'    => $relatedID,
        		'reason'        => !empty($_POST['reason']) ? trim(($_POST['reason'])) : $reasons[$reasonID],
        		'create_time'   => time(),
        	);
        	$this->Report->add($data);
         	$this->success(L('abuse_reported'));
    	}
    	else
    	{
    	    $this->assign('type', $type);
    	    $this->assign('rid', $relatedID);
    	    $this->assign('reasons', $reasons);
    	    $this->assign('captcha', $captcha);
            exit($this->fetch());
    	}
    }

}
//End Class


/* End of file ReportAction.class.php */
/* Location: ./App/Lib/Action/Home/ReportAction.class.php */

